package com.yhp.dao.impl;

import com.yhp.bean.Menu;
import com.yhp.bean.Role;
import com.yhp.bean.Users;
import com.yhp.dao.DBUtils;
import com.yhp.dao.MenuDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MenuDaoImpl extends DBUtils implements MenuDao {
    @Override
    public List<Menu> getMenuList() {
        List<Menu> menuList=new ArrayList<>();
        try {
            String sql="select * from menu";
            resultSet = query(sql, null);
            while(resultSet.next()){
                Menu menu = new Menu();
                menu.setMenuId(resultSet.getInt("menuid"));
                menu.setMenuName(resultSet.getString("menuname"));
                menu.setUpmenuId(resultSet.getInt("upmenuid"));
                menu.setState(resultSet.getInt("state"));
                menu.setDescs(resultSet.getString("descs"));
                menu.setUrl(resultSet.getString("url"));
                menuList.add(menu);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return menuList;
    }

    @Override
    public Menu getMenuById(int menuid) {
//        List<Menu> menuList=new ArrayList<>();
        Menu menu = new Menu();
        try {
            String sql = "select * from menu where menuid=?";
            List params=new ArrayList();
            params.add(menuid);
            resultSet = query(sql, params);
            while (resultSet.next()){

                menu.setMenuId(resultSet.getInt("menuid"));

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return menu;
    }

    @Override
    public List<Menu> getMenuLists(int pageIndex, int pageSize) {
        List<Menu> menuList=new ArrayList<Menu>();
        try {
            String sql = "select * from menu limit ?,?";
            List params=new ArrayList();
            params.add((pageIndex-1)*pageSize);
            params.add(pageSize);

            resultSet = query(sql, params);
            while(resultSet.next()){
                Menu menu = new Menu();
                menu.setMenuId(resultSet.getInt("menuid"));
                menu.setMenuName(resultSet.getString("menuname"));
                menu.setUpmenuId(resultSet.getInt("upmenuid"));
                menu.setState(resultSet.getInt("state"));
                menu.setDescs(resultSet.getString("descs"));
                menu.setUrl(resultSet.getString("url"));

                menuList.add(menu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return menuList;
    }

    @Override
    public int total() {
        int total=0;
        try {
            String sql="select count(1) from role r  ";
            resultSet = query(sql, null);
            while(resultSet.next()){
                total=resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll();
        }
        return total;
    }

    @Override
    public int insertMenu(Menu menu) {
        int update = 0;
        try {
            String sql = "insert into menu values(null,?,?,?,?,?)";
            List params = new ArrayList();
            params.add(menu.getMenuName());
            params.add(menu.getUpmenuId());
            params.add(menu.getState());
            params.add(menu.getDescs());
            params.add(menu.getUrl());
            update = update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return update;
    }

    @Override
    public Menu findById(int mid) {
        Menu menu = new Menu();
        try {
            String sql = "select * from menu where menuid=?";
            List params=new ArrayList();
            params.add(mid);
            resultSet = query(sql, params);
            while (resultSet.next()){
                menu.setMenuId(resultSet.getInt("menuid"));
                menu.setMenuName(resultSet.getString("menuname"));
                menu.setUpmenuId(resultSet.getInt("upmenuid"));
                menu.setState(resultSet.getInt("state"));
                menu.setDescs(resultSet.getString("descs"));
                menu.setUrl(resultSet.getString("url"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return menu;
    }

    @Override
    public int updateMenu(Menu menu) {
        int update = 0;
        try {
            String sql = "update menu set menuname=?,upmenuid=?,state=?,descs=?,url=? where menuid=?";
            List params=new ArrayList();
            params.add(menu.getMenuName());
            params.add(menu.getUpmenuId());
            params.add(menu.getState());
            params.add(menu.getDescs());
            params.add(menu.getUrl());
            params.add(menu.getMenuId());
            update = update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return update;
    }

    @Override
    public int delMenu(int mid) {
        int i = 0;
        try {
            String sql="delete from menu WHERE menuid=?";
            List params=new ArrayList();
            params.add(mid);
            i = update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return i;
    }
}
